package org.openCage.artig;

import org.openCage.artig.stjx.Artig;
import org.openCage.artig.stjx.ArtigFromXML;
import org.openCage.artig.stjx.Deployed;
import java.io.InputStream;

public class GetDeployed {
    private String name;

    public GetDeployed( String name ) {
        this.name = name;
    }

    public InputStream getIS() {
        return getClass().getResourceAsStream( "/META-INF/" + name + ".artig" );
    }

    public Deployed get() {


        InputStream is = getClass().getResourceAsStream( "/META-INF/" + name + ".artig" );

        if ( is == null ) {
            throw new IllegalStateException("this module/jar does not have a <" + name+ ">.artig file, i.e. not a artig project");
        }


        try {
            Artig ad = ArtigFromXML.read( is );
            if ( !(ad.getKind() instanceof Deployed )) {
                throw new IllegalArgumentException("wrong kind of Artig, expected Deployed) got " + ad.getKind().getClass().getName());
            }

            return (Deployed)ad.getKind();

        } catch ( Error e ) {
            System.err.println("errror reading: " + name + "-deployed.artig" );
            e.getCause().printStackTrace();
            throw e;
        }

    }
}
